﻿using System.Collections.Generic;
using System.Text;
using Nancy;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace GeometryServer.Services
{
    public static class Utilities
    {
        public static dynamic GetGeometries(string GeometryString)
        {
            JObject o = JObject.Parse(GeometryString);
            dynamic inputgeometrylist;
            string geometrytype = (string)o["geometryType"];

            JArray geometries = (JArray)o["geometries"];
            switch (geometrytype)
            {
                case "GeometryPoint":
                case "esriGeometryPoint":
                    inputgeometrylist = new List<DotSpatial.Topology.Point>();
                    for (int i = 0; i < geometries.Count; i++)
                    {
                        var p = geometries[i].ToObject<GISServer.Core.Client.Geometry.Point>();
                        inputgeometrylist.Add(new DotSpatial.Topology.Point(p.X, p.Y));
                    }
                    return inputgeometrylist;
                case "GeometryMultipoint":
                case "esriGeometryMultipoint":
                    inputgeometrylist = new List<DotSpatial.Topology.MultiPoint>();
                    for (int i = 0; i < geometries.Count; i++)
                    {
                        var coorlist = new DotSpatial.Topology.CoordinateList();
                        var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.MultiPoint>();
                        for (int j = 0; j < mp.Points.Count; j++)
                        {
                            coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Points[j].X, mp.Points[j].Y));

                        }
                        inputgeometrylist.Add(new DotSpatial.Topology.MultiPoint(coorlist));
                    }
                    return inputgeometrylist;
                case "GeometryPolyline":
                case "esriGeometryPolyline":
                    inputgeometrylist = new List<DotSpatial.Topology.MultiLineString>();
                    for (int i = 0; i < geometries.Count; i++)
                    {
                        var ml = new DotSpatial.Topology.MultiLineString();
                        var lineStrings = new List<DotSpatial.Topology.LineString>();
                        var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.Polyline>();
                        for (int j = 0; j < mp.Paths.Count; j++)
                        {
                            var coorlist = new DotSpatial.Topology.CoordinateList();
                            for (int k = 0; k < mp.Paths[j].Count; k++)
                            {
                                coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Paths[j][k].X, mp.Paths[j][k].Y));
                            }
                            lineStrings.Add(new DotSpatial.Topology.LineString(coorlist));
                        }
                        inputgeometrylist.Add(new DotSpatial.Topology.MultiLineString(lineStrings));
                    }
                    return inputgeometrylist;
                case "GeometryPolygon":
                case "esriGeometryPolygon":
                    inputgeometrylist = new List<DotSpatial.Topology.MultiPolygon>();
                    for (int i = 0; i < geometries.Count; i++)
                    {
                        var ml = new DotSpatial.Topology.MultiLineString();
                        var shellstring = new List<DotSpatial.Topology.Polygon>();
                        var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.Polygon>();
                        for (int j = 0; j < mp.Rings.Count; j++)
                        {
                            var coorlist = new DotSpatial.Topology.CoordinateList();
                            for (int k = 0; k < mp.Rings[j].Count; k++)
                            {
                                coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Rings[j][k].X, mp.Rings[j][k].Y));
                            }
                            shellstring.Add(new DotSpatial.Topology.Polygon(new DotSpatial.Topology.LinearRing(coorlist)));
                        }
                        inputgeometrylist.Add(new DotSpatial.Topology.MultiPolygon(shellstring.ToArray()));
                    }
                    return inputgeometrylist;
                case "GeometryEnvelope":
                case "esriGeometryEnvelope":
                    inputgeometrylist = new List<DotSpatial.Topology.Envelope>();
                    for (int i = 0; i < geometries.Count; i++)
                    {
                        var p = geometries[i].ToObject<GISServer.Core.Client.Geometry.Envelope>();
                        inputgeometrylist.Add(new DotSpatial.Topology.Envelope(p.XMin, p.XMax, p.YMin, p.YMax));
                    }
                    return inputgeometrylist;
                default:
                    break;
            }
            return null;
        }

        public static dynamic GetGeometry(string GeometryString, string geometrytype)
        {
            JObject o = JObject.Parse(GeometryString);
            dynamic inputgeometrylist;

            switch (geometrytype)
            {
                case "GeometryPoint":
                case "esriGeometryPoint":
                    {
                        JArray geometries = (JArray)o["geometries"];
                        inputgeometrylist = new List<DotSpatial.Topology.Point>();
                        for (int i = 0; i < geometries.Count; i++)
                        {
                            var p = geometries[i].ToObject<GISServer.Core.Client.Geometry.Point>();
                            inputgeometrylist.Add(new DotSpatial.Topology.Point(p.X, p.Y));
                        }
                        return inputgeometrylist;
                    }
                case "GeometryMultipoint":
                case "esriGeometryMultipoint":
                    {
                        JArray geometries = (JArray)o["geometries"];
                        inputgeometrylist = new List<DotSpatial.Topology.MultiPoint>();
                        for (int i = 0; i < geometries.Count; i++)
                        {
                            var coorlist = new DotSpatial.Topology.CoordinateList();
                            var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.MultiPoint>();
                            for (int j = 0; j < mp.Points.Count; j++)
                            {
                                coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Points[j].X, mp.Points[j].Y));

                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiPoint(coorlist));
                        }
                        return inputgeometrylist;
                    }
                case "GeometryPolyline":
                case "esriGeometryPolyline":
                    {
                        JArray geometries = (JArray)o["geometries"];
                        inputgeometrylist = new List<DotSpatial.Topology.MultiLineString>();
                        for (int i = 0; i < geometries.Count; i++)
                        {
                            var ml = new DotSpatial.Topology.MultiLineString();
                            var lineStrings = new List<DotSpatial.Topology.LineString>();
                            var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.Polyline>();
                            for (int j = 0; j < mp.Paths.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < mp.Paths[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Paths[j][k].X, mp.Paths[j][k].Y));
                                }
                                lineStrings.Add(new DotSpatial.Topology.LineString(coorlist));
                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiLineString(lineStrings));
                        }
                        return inputgeometrylist;
                    }
                case "GeometryPolygon":
                case "esriGeometryPolygon":
                    {
                        JArray geometries = (JArray)o["rings"];
                        inputgeometrylist = new List<DotSpatial.Topology.MultiPolygon>();
                        for (int i = 0; i < geometries.Count; i++)
                        {
                            var ml = new DotSpatial.Topology.MultiLineString();
                            var shellstring = new List<DotSpatial.Topology.Polygon>();
                            var mp = geometries[i].ToObject<GISServer.Core.Client.Geometry.Polygon>();
                            for (int j = 0; j < mp.Rings.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < mp.Rings[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(mp.Rings[j][k].X, mp.Rings[j][k].Y));
                                }
                                shellstring.Add(new DotSpatial.Topology.Polygon(new DotSpatial.Topology.LinearRing(coorlist)));
                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiPolygon(shellstring.ToArray()));
                        }
                        return inputgeometrylist;
                    }
                case "GeometryEnvelope":
                case "esriGeometryEnvelope":
                    {
                        JArray geometries = (JArray)o["geometries"];
                        inputgeometrylist = new List<DotSpatial.Topology.Envelope>();
                        for (int i = 0; i < geometries.Count; i++)
                        {
                            var p = geometries[i].ToObject<GISServer.Core.Client.Geometry.Envelope>();
                            inputgeometrylist.Add(new DotSpatial.Topology.Envelope(p.XMin, p.XMax, p.YMin, p.YMax));
                        }
                        return inputgeometrylist;
                    }
                default:
                    break;
            }
            return null;
        }

        public static dynamic GetGISGeometries(dynamic Geometry)
        {
            var geometries = new GISServer.Core.Client.Geometry.GeometryArray();
            var geometrieslist = new List<GISServer.Core.Client.Geometry.Geometry>();
            if (Geometry is DotSpatial.Topology.Point)
            {
                var point = new GISServer.Core.Client.Geometry.Point(Geometry.X, Geometry.Y);
                geometrieslist.Add(point);
                geometries.GeometryType = "GeometryPoint";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is DotSpatial.Topology.MultiPoint)
            {
                var multipoint = new GISServer.Core.Client.Geometry.MultiPoint();
                multipoint.Points = new List<GISServer.Core.Client.Geometry.Point>();
                foreach (var coordinate in Geometry.Coordinates)
                {
                    var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                    multipoint.Points.Add(point);
                }
                geometrieslist.Add(multipoint);
                geometries.GeometryType = "GeometryMultipoint";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is DotSpatial.Topology.MultiLineString)
            {
                var multiline = new GISServer.Core.Client.Geometry.Polyline();
                multiline.Paths = new List<GISServer.Core.Client.Geometry.PointCollection>();

                for (int i = 0; i < Geometry.Count; i++)
                {
                    var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                    foreach (var coordinate in Geometry[i].Coordinates)
                    {
                        var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                        pointcollection.Add(point);
                    }
                    multiline.Paths.Add(pointcollection);
                }
                geometrieslist.Add(multiline);
                geometries.GeometryType = "GeometryPolyline";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is DotSpatial.Topology.Polygon)
            {
                var polygon = new GISServer.Core.Client.Geometry.Polygon();
                polygon.Rings = new List<GISServer.Core.Client.Geometry.PointCollection>();
                var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                foreach (var coordinate in Geometry.Coordinates)
                {
                    var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                    pointcollection.Add(point);
                }
                polygon.Rings.Add(pointcollection);
                geometrieslist.Add(polygon);
                geometries.GeometryType = "GeometryPolygon";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is DotSpatial.Topology.MultiPolygon)
            {
                var polygon = new GISServer.Core.Client.Geometry.Polygon();
                polygon.Rings = new List<GISServer.Core.Client.Geometry.PointCollection>();

                for (int i = 0; i < Geometry.Count; i++)
                {
                    var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                    foreach (var coordinate in Geometry[i].Coordinates)
                    {
                        var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                        pointcollection.Add(point);
                    }
                    polygon.Rings.Add(pointcollection);
                }
                geometrieslist.Add(polygon);
                geometries.GeometryType = "GeometryPolygon";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is DotSpatial.Topology.Envelope)
            {
                var envelope = new GISServer.Core.Client.Geometry.Envelope(Geometry.Minimum.X, Geometry.Minimum.Y, Geometry.Maximum.X, Geometry.Minimum.Y);
                geometrieslist.Add(envelope);
                geometries.GeometryType = "GeometryEnvelope";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            else if (Geometry is List<DotSpatial.Topology.IGeometry>)
            {
                foreach (var geometry in Geometry)
                {
                    var polygon = new GISServer.Core.Client.Geometry.Polygon();
                    polygon.Rings = new List<GISServer.Core.Client.Geometry.PointCollection>();
                    var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                    foreach (var coordinate in geometry.Coordinates)
                    {
                        var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                        pointcollection.Add(point);
                    }
                    polygon.Rings.Add(pointcollection);
                    geometrieslist.Add(polygon);
                }

                geometries.GeometryType = "GeometryPolygon";
                geometries.Geometries = geometrieslist;
                return geometries;
            }
            return null;
        }


        public static string getJSON(dynamic Result)
        {
            if ((Result is GISServer.Core.Client.Geometry.GeometryArray) || (Result is GISServer.Core.Client.Geometry.SingleGeometry) || (Result is GISServer.Core.Client.GeometryService.AreasAndLengths) || (Result is GISServer.Core.Client.GeometryService.Root) || (Result is GISServer.Core.Catalog.Catalog))
            {
                return JsonConvert.SerializeObject(Result, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() });
            }
            return null;
        }
        public static string getPJSON(dynamic Result)
        {
            if ((Result is GISServer.Core.Client.Geometry.GeometryArray) || (Result is GISServer.Core.Client.Geometry.SingleGeometry) || (Result is GISServer.Core.Client.GeometryService.AreasAndLengths) || (Result is GISServer.Core.Client.GeometryService.Root) || (Result is GISServer.Core.Catalog.Catalog))
            {
                return JsonConvert.SerializeObject(Result, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() });
            }
            return null;
        }


        public static Nancy.Response GetJsonResponseObject(string resultstring)
        {
            var jsonBytes = Encoding.UTF8.GetBytes(resultstring);
            return new Response
            {
                ContentType = "application/json",
                StatusCode = HttpStatusCode.OK,
                Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length)
            };
        }

        public static DotSpatialGeometryArray getDotSpatialGeometryArray(GISServer.Core.Client.Geometry.GeometryArray ServerGeometryArray)
        {
            dynamic inputgeometrylist;
            switch (ServerGeometryArray.GeometryType)
            {
                case "esriGeometryPoint":
                    {
                        var inputgeom = (List<GISServer.Core.Client.Geometry.Point>)ServerGeometryArray.Geometries;
                        inputgeometrylist = new List<DotSpatial.Topology.Point>();
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            inputgeometrylist.Add(new DotSpatial.Topology.Point(inputgeom[i].X, inputgeom[i].Y));
                        }
                        var dotspatial_geom_array = new DotSpatialGeometryArray();
                        dotspatial_geom_array.Geometries = inputgeometrylist;
                        dotspatial_geom_array.GeometryType = ServerGeometryArray.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryMultipoint":
                    {
                        var inputgeom = (List<GISServer.Core.Client.Geometry.MultiPoint>)ServerGeometryArray.Geometries;
                        inputgeometrylist = new List<DotSpatial.Topology.MultiPoint>();
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var coorlist = new DotSpatial.Topology.CoordinateList();
                            for (int j = 0; j < inputgeom[i].Points.Count; j++)
                            {
                                coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom[i].Points[j].X, inputgeom[i].Points[j].Y));
                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiPoint(coorlist));
                        }
                        var dotspatial_geom_array = new DotSpatialGeometryArray();
                        dotspatial_geom_array.Geometries = inputgeometrylist;
                        dotspatial_geom_array.GeometryType = ServerGeometryArray.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryPolyline":
                    {
                        var inputgeom = (List<GISServer.Core.Client.Geometry.Polyline>)ServerGeometryArray.Geometries;
                        inputgeometrylist = new List<DotSpatial.Topology.MultiLineString>();
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var ml = new DotSpatial.Topology.MultiLineString();
                            var lineStrings = new List<DotSpatial.Topology.LineString>();
                            for (int j = 0; j < inputgeom[i].Paths.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < inputgeom[i].Paths[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom[i].Paths[j][k].X, inputgeom[i].Paths[j][k].Y));
                                }
                                lineStrings.Add(new DotSpatial.Topology.LineString(coorlist));
                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiLineString(lineStrings));
                        }
                        var dotspatial_geom_array = new DotSpatialGeometryArray();
                        dotspatial_geom_array.Geometries = inputgeometrylist;
                        dotspatial_geom_array.GeometryType = ServerGeometryArray.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryPolygon":
                    {
                        var inputgeom = (List<GISServer.Core.Client.Geometry.Polygon>)ServerGeometryArray.Geometries;
                        inputgeometrylist = new List<DotSpatial.Topology.MultiPolygon>();
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var shellstring = new List<DotSpatial.Topology.Polygon>();
                            for (int j = 0; j < inputgeom[i].Rings.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < inputgeom[i].Rings[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom[i].Rings[j][k].X, inputgeom[i].Rings[j][k].Y));
                                }
                                shellstring.Add(new DotSpatial.Topology.Polygon(new DotSpatial.Topology.LinearRing(coorlist)));
                            }
                            inputgeometrylist.Add(new DotSpatial.Topology.MultiPolygon(shellstring.ToArray()));
                        }
                        var dotspatial_geom_array = new DotSpatialGeometryArray();
                        dotspatial_geom_array.Geometries = inputgeometrylist;
                        dotspatial_geom_array.GeometryType = ServerGeometryArray.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryEnvelope":
                    {
                        var inputgeom = (List<GISServer.Core.Client.Geometry.Envelope>)ServerGeometryArray.Geometries;
                        inputgeometrylist = new List<DotSpatial.Topology.Envelope>();
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            inputgeometrylist.Add(new DotSpatial.Topology.Envelope(inputgeom[i].XMin, inputgeom[i].XMax, inputgeom[i].YMin, inputgeom[i].YMax));
                        }
                        var dotspatial_geom_array = new DotSpatialGeometryArray();
                        dotspatial_geom_array.Geometries = inputgeometrylist;
                        dotspatial_geom_array.GeometryType = ServerGeometryArray.GeometryType;
                        return dotspatial_geom_array;
                    }
                default:
                    break;
            }
            return null;
        }

        public static DotSpatialSingleGeometry getDotSpatialSingleGeometry(GISServer.Core.Client.Geometry.SingleGeometry SingleGeometry)
        {
            DotSpatialSingleGeometry dotspatial_geom_array;
            switch (SingleGeometry.GeometryType)
            {
                case "esriGeometryPoint":
                    {
                        var inputgeom = (GISServer.Core.Client.Geometry.Point)SingleGeometry.geometry;
                        dotspatial_geom_array = new DotSpatialSingleGeometry();
                        dotspatial_geom_array.Geometry = new DotSpatial.Topology.Point(inputgeom.X, inputgeom.Y); ;
                        dotspatial_geom_array.GeometryType = SingleGeometry.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryMultipoint":
                    {
                        var inputgeom = (GISServer.Core.Client.Geometry.MultiPoint)SingleGeometry.geometry;


                        var coorlist = new DotSpatial.Topology.CoordinateList();
                        for (int j = 0; j < inputgeom.Points.Count; j++)
                        {
                            coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom.Points[j].X, inputgeom.Points[j].Y));
                        }


                        dotspatial_geom_array = new DotSpatialSingleGeometry();
                        dotspatial_geom_array.Geometry = new DotSpatial.Topology.MultiPoint(coorlist);
                        dotspatial_geom_array.GeometryType = SingleGeometry.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryPolyline":
                    {
                        var inputgeom = (GISServer.Core.Client.Geometry.Polyline)SingleGeometry.geometry;
                        
                            var ml = new DotSpatial.Topology.MultiLineString();
                            var lineStrings = new List<DotSpatial.Topology.LineString>();
                            for (int j = 0; j < inputgeom.Paths.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < inputgeom.Paths[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom.Paths[j][k].X, inputgeom.Paths[j][k].Y));
                                }
                                lineStrings.Add(new DotSpatial.Topology.LineString(coorlist));
                            }
                            
                        
                        dotspatial_geom_array = new DotSpatialSingleGeometry();
                        dotspatial_geom_array.Geometry = new DotSpatial.Topology.MultiLineString(lineStrings);
                        dotspatial_geom_array.GeometryType = SingleGeometry.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryPolygon":
                    {
                        var inputgeom = (GISServer.Core.Client.Geometry.Polygon)SingleGeometry.geometry;
                        
                        
                            var shellstring = new List<DotSpatial.Topology.Polygon>();
                            for (int j = 0; j < inputgeom.Rings.Count; j++)
                            {
                                var coorlist = new DotSpatial.Topology.CoordinateList();
                                for (int k = 0; k < inputgeom.Rings[j].Count; k++)
                                {
                                    coorlist.Add(new DotSpatial.Topology.Coordinate(inputgeom.Rings[j][k].X, inputgeom.Rings[j][k].Y));
                                }
                                shellstring.Add(new DotSpatial.Topology.Polygon(new DotSpatial.Topology.LinearRing(coorlist)));
                            }
                            
                        
                        dotspatial_geom_array = new DotSpatialSingleGeometry();
                        dotspatial_geom_array.Geometry = new DotSpatial.Topology.MultiPolygon(shellstring.ToArray());
                        dotspatial_geom_array.GeometryType = SingleGeometry.GeometryType;
                        return dotspatial_geom_array;
                    }
                case "esriGeometryEnvelope":
                    {
                        var inputgeom = (GISServer.Core.Client.Geometry.Envelope)SingleGeometry.geometry;
                        dotspatial_geom_array = new DotSpatialSingleGeometry();
                        dotspatial_geom_array.Geometry = new DotSpatial.Topology.Envelope(inputgeom.XMin, inputgeom.XMax, inputgeom.YMin, inputgeom.YMax);
                        dotspatial_geom_array.GeometryType = SingleGeometry.GeometryType;
                        return dotspatial_geom_array;
                    }
                default:
                    break;
            }
            return null;
        }

        public static GISServer.Core.Client.Geometry.GeometryArray getServerGeometryArray(DotSpatialGeometryArray dotspatial_geom_array)
        {
            var server_geometry_array = new GISServer.Core.Client.Geometry.GeometryArray();
            switch (dotspatial_geom_array.GeometryType)
            {
                case "esriGeometryPoint":
                    {
                        server_geometry_array.GeometryType = dotspatial_geom_array.GeometryType;
                        server_geometry_array.Geometries = new List<GISServer.Core.Client.Geometry.Point>();
                        var inputgeom = (List<DotSpatial.Topology.Point>)dotspatial_geom_array.Geometries;
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            server_geometry_array.Geometries.Add(new GISServer.Core.Client.Geometry.Point(inputgeom[i].X, inputgeom[i].Y));
                        }
                        return server_geometry_array;
                    }
                case "esriGeometryMultipoint":
                    {
                        server_geometry_array.GeometryType = dotspatial_geom_array.GeometryType;
                        server_geometry_array.Geometries = new List<GISServer.Core.Client.Geometry.MultiPoint>();
                        var inputgeom = (List<DotSpatial.Topology.MultiPoint>)dotspatial_geom_array.Geometries;
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var multipoint = new GISServer.Core.Client.Geometry.MultiPoint();
                            for (int j = 0; j < inputgeom[i].Coordinates.Count; j++)
                            {
                                multipoint.Points.Add(new GISServer.Core.Client.Geometry.Point(inputgeom[i].Coordinates[j].X, inputgeom[i].Coordinates[j].Y));
                            }
                            server_geometry_array.Geometries.Add(multipoint);
                        }
                        return server_geometry_array;
                    }
                case "esriGeometryPolyline":
                    {
                        server_geometry_array.GeometryType = dotspatial_geom_array.GeometryType;
                        server_geometry_array.Geometries = new List<GISServer.Core.Client.Geometry.Polyline>();
                        var inputgeom = (List<DotSpatial.Topology.MultiLineString>)dotspatial_geom_array.Geometries;
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var polyline = new GISServer.Core.Client.Geometry.Polyline();
                            for (int j = 0; j < inputgeom[i].NumGeometries; j++)
                            {
                                var pc = new GISServer.Core.Client.Geometry.PointCollection();
                                for (int k = 0; k < inputgeom[i][j].Coordinates.Count; k++)
                                {
                                    pc.Add(new GISServer.Core.Client.Geometry.Point(inputgeom[i][j].Coordinates[k].X, inputgeom[i][j].Coordinates[k].Y));
                                }
                                polyline.Paths.Add(pc);
                            }
                            server_geometry_array.Geometries.Add(polyline);
                        }
                        return server_geometry_array;
                    }
                case "esriGeometryPolygon":
                    {
                        server_geometry_array.GeometryType = dotspatial_geom_array.GeometryType;
                        server_geometry_array.Geometries = new List<GISServer.Core.Client.Geometry.Polygon>();
                        var inputgeom = (List<DotSpatial.Topology.MultiPolygon>)dotspatial_geom_array.Geometries;
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            var polyline = new GISServer.Core.Client.Geometry.Polygon();
                            for (int j = 0; j < inputgeom[i].NumGeometries; j++)
                            {
                                var pc = new GISServer.Core.Client.Geometry.PointCollection();
                                for (int k = 0; k < inputgeom[i][j].Coordinates.Count; k++)
                                {
                                    pc.Add(new GISServer.Core.Client.Geometry.Point(inputgeom[i][j].Coordinates[k].X, inputgeom[i][j].Coordinates[k].Y));
                                }
                                polyline.Rings.Add(pc);
                            }
                            server_geometry_array.Geometries.Add(polyline);
                        }
                        return server_geometry_array;
                    }
                case "esriGeometryEnvelope":
                    {
                        server_geometry_array.GeometryType = dotspatial_geom_array.GeometryType;
                        server_geometry_array.Geometries = new List<GISServer.Core.Client.Geometry.Envelope>();
                        var inputgeom = (List<DotSpatial.Topology.Envelope>)dotspatial_geom_array.Geometries;
                        for (int i = 0; i < inputgeom.Count; i++)
                        {
                            server_geometry_array.Geometries.Add(new GISServer.Core.Client.Geometry.Envelope(inputgeom[i].Minimum.X, inputgeom[i].Minimum.Y, inputgeom[i].Maximum.X, inputgeom[i].Minimum.Y));
                        }
                        return server_geometry_array;
                    }
                default:
                    break;
            }
            return null;
        }

        public static List<DotSpatial.Topology.MultiPolygon> getDotSpatialPolygonArray(List<GISServer.Core.Client.Geometry.Polygon> polygonlist)
        {
            var dotspatialpolygonarray = new List<DotSpatial.Topology.MultiPolygon>();
            dotspatialpolygonarray = new List<DotSpatial.Topology.MultiPolygon>();
            for (int i = 0; i < polygonlist.Count; i++)
            {
                var shellstring = new List<DotSpatial.Topology.Polygon>();
                for (int j = 0; j < polygonlist[i].Rings.Count; j++)
                {
                    var coorlist = new DotSpatial.Topology.CoordinateList();
                    for (int k = 0; k < polygonlist[i].Rings[j].Count; k++)
                    {
                        coorlist.Add(new DotSpatial.Topology.Coordinate(polygonlist[i].Rings[j][k].X, polygonlist[i].Rings[j][k].Y));
                    }
                    shellstring.Add(new DotSpatial.Topology.Polygon(new DotSpatial.Topology.LinearRing(coorlist)));
                }
                dotspatialpolygonarray.Add(new DotSpatial.Topology.MultiPolygon(shellstring.ToArray()));
            }
            return dotspatialpolygonarray;
        }
        public static List<DotSpatial.Topology.MultiLineString> getDotSpatialPolyLineArray(List<GISServer.Core.Client.Geometry.Polyline> polylinelist)
        {
            var dotspatialpolylinearray = new List<DotSpatial.Topology.MultiLineString>();
            for (int i = 0; i < polylinelist.Count; i++)
            {
                var linestrings = new List<DotSpatial.Topology.LineString>();
                for (int j = 0; j < polylinelist[i].Paths.Count; j++)
                {
                    var coorlist = new DotSpatial.Topology.CoordinateList();
                    for (int k = 0; k < polylinelist[i].Paths[j].Count; k++)
                    {
                        coorlist.Add(new DotSpatial.Topology.Coordinate(polylinelist[i].Paths[j][k].X, polylinelist[i].Paths[j][k].Y));
                    }
                    linestrings.Add(new DotSpatial.Topology.LineString(coorlist));
                }
                dotspatialpolylinearray.Add(new DotSpatial.Topology.MultiLineString(linestrings.ToArray()));
            }
            return dotspatialpolylinearray;
        }

        public static dynamic GetGISSingleGeometry(dynamic Geometry)
        {
            var singleGeometry = new GISServer.Core.Client.Geometry.SingleGeometry();
            if (Geometry is DotSpatial.Topology.Point)
            {
                var point = new GISServer.Core.Client.Geometry.Point(Geometry.X, Geometry.Y);
                singleGeometry.GeometryType = "esriGeometryPoint";
                singleGeometry.geometry = point;
                return singleGeometry;
            }
            else if (Geometry is DotSpatial.Topology.MultiPoint)
            {
                var multipoint = new GISServer.Core.Client.Geometry.MultiPoint();
                multipoint.Points = new List<GISServer.Core.Client.Geometry.Point>();
                foreach (var coordinate in Geometry.Coordinates)
                {
                    var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                    multipoint.Points.Add(point);
                }
                singleGeometry.GeometryType = "esriGeometryMultipoint";
                singleGeometry.geometry = multipoint;
                return singleGeometry;
            }
            else if (Geometry is DotSpatial.Topology.MultiLineString)
            {
                var multiline = new GISServer.Core.Client.Geometry.Polyline();
                for (int i = 0; i < Geometry.Count; i++)
                {
                    var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                    foreach (var coordinate in Geometry[i].Coordinates)
                    {
                        var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                        pointcollection.Add(point);
                    }
                    multiline.Paths.Add(pointcollection);
                }
                singleGeometry.GeometryType = "esriGeometryPolyline";
                singleGeometry.geometry = multiline;
                return singleGeometry;
            }
            else if (Geometry is DotSpatial.Topology.Polygon)
            {
                var polygon = new GISServer.Core.Client.Geometry.Polygon();
                var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                foreach (var coordinate in Geometry.Coordinates)
                {
                    var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                    pointcollection.Add(point);
                }
                polygon.Rings.Add(pointcollection);
                singleGeometry.GeometryType = "esriGeometryPolygon";
                singleGeometry.geometry = polygon;
                return singleGeometry;
            }
            else if (Geometry is DotSpatial.Topology.MultiPolygon)
            {
                var polygon = new GISServer.Core.Client.Geometry.Polygon();
                for (int i = 0; i < Geometry.Count; i++)
                {
                    var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();
                    foreach (var coordinate in Geometry[i].Coordinates)
                    {
                        var point = new GISServer.Core.Client.Geometry.Point(coordinate.X, coordinate.Y);
                        pointcollection.Add(point);
                    }
                    polygon.Rings.Add(pointcollection);
                }
                singleGeometry.GeometryType = "esriGeometryPolygon";
                singleGeometry.geometry = polygon;
                return singleGeometry;
            }
            else if (Geometry is DotSpatial.Topology.Envelope)
            {
                var envelope = new GISServer.Core.Client.Geometry.Envelope(Geometry.Minimum.X, Geometry.Minimum.Y, Geometry.Maximum.X, Geometry.Minimum.Y);
                singleGeometry.GeometryType = "esriGeometryEnvelope";
                singleGeometry.geometry = envelope;
                return singleGeometry;
            }
            return null;
        }

    }
}